<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>百度地图地址及经纬度查询样例</title>
<script src="http://api.map.baidu.com/api?v=3.0&ak=NjVbTcGefiMIvaLsTuOGDFkbqhS92Eho"></script>
<script type="text/javascript" src="http://api.map.baidu.com/library/SearchControl/1.4/src/SearchControl_min.js"></script>
<link rel="stylesheet" href="http://api.map.baidu.com/library/SearchControl/1.4/src/SearchControl_min.css" />
<style type="text/css">  
  html{height:100%;}  
  body{height:100%; margin-left: 50px;}  
  #mymap{width:800px; height:450px; margin-top: 20px;}
  #searchBox, #rsltArea {margin-top: 20px;}
</style> 
</head>
<body>
  <div id="searchBox">
    <input type="text" id="txtSearch" style="width:200px;"></input>
    <input type="button" onclick="searchAddrAndLngLat()" value="查询"></input>
  </div>

  <div id="mymap"></div>
  
  <div id="rsltArea">
    <span style="margin-right: 10px;">地址</span><input type="text" id="txtAddr"></input>
    <span style="margin-right: 10px;">Lat</span><input type="text" id="txtLat"></input>
    <span style="margin-right: 10px;">Lng</span><input type="text" id="txtLng"></input>
  </div>

</body>
<script type="text/javascript">
  // 定义并初始化地图  
  var map = new BMap.Map("mymap");
  map.centerAndZoom("南京", 15);
  map.enableInertialDragging();
  map.enableScrollWheelZoom(true);

  // 用于搜索地址及经纬度
  // 地址输入自动提示控件定义
  var ac = new BMap.Autocomplete({
      "input" : "txtSearch",
      "location" : map
  });
  // 下拉框选择地址后自动获得地址和经纬度
  ac.addEventListener("onconfirm", function(e) {
      searchAddrAndLngLat();
  });
  // 地址检索控件定义
  var local = new BMap.LocalSearch(map, {
    onSearchComplete: function() {
      if (local.getStatus() == BMAP_STATUS_SUCCESS){
        var pp = local.getResults().getPoi(0);
        setPlace(pp.address, pp.point);
      }
    }
  });
  // 查询地址及经纬度
  function searchAddrAndLngLat() {
    var p = document.getElementById("txtSearch").value;
    local.search(p);
  }

  // 用于点击地图获得地址和经纬度坐标
  // 逆地址解析器
  var geoc = new BMap.Geocoder();
  // 鼠标点击获得地址信息
  map.addEventListener("click", function(e) {
    geoc.getLocation(e.point, function(rs){
        setPlace(rs.address, e.point);
    });
  });

  // 设置数据
  function setPlace(addr, point) {
      map.clearOverlays();
      map.centerAndZoom(point, 15);
      map.addOverlay(new BMap.Marker(point));
      document.getElementById("txtAddr").value=addr;
      document.getElementById("txtLat").value=point.lat;
      document.getElementById("txtLng").value=point.lng;
  }

</script>
</html>